import { ConfigureService } from '@app/configure';
import { Injectable } from '@nestjs/common';
import { JwtVerifyService } from '@app/jwt-verify';
import AlipaySdk from 'alipay-sdk';

@Injectable()
export class LoginService {
    constructor(
        private readonly config:ConfigureService,
        public readonly jwt:JwtVerifyService,
    ){}

    async loginAli(code:string) {
        // const conf:any = await this.common.queryPlatformData('alipay')
        // if(!conf) return null
        // const alipaySdk = new AlipaySdk({
        //     appId: conf.appId,
        //     privateKey : conf.privateKey,
        //     alipayPublicKey : conf.alipayPublicKey
        //   });


        // return alipaySdk.exec('alipay.system.oauth.token', {
        //     app_id : conf.appId,
        //     format : 'json',
        //     sign_type : 'RSA2',
        //     grant_type : 'authorization_code',
        //     code : code
        // })
    }

    makeToken(id:number, time?:number, jwtConfig?:any) {
        if(!time) {
            time = Math.floor(Date.now() / 1000)
        }

        if(!jwtConfig) {
            jwtConfig = this.config.get('jwt.userClient')
        }


        let payload = {key : id * jwtConfig.login.scale + jwtConfig.login.scale, id : Math.ceil((time - 1030) / 263), time : time}
        let accessToken = this.jwt.sign(payload, {
            secret : jwtConfig.login.secret,
            expiresIn : jwtConfig.expire
        })

        let refreshToken = this.jwt.sign({key :id  * jwtConfig.refreshLogin.scale - jwtConfig.refreshLogin.scale, rid : Math.ceil((time - 8030) / 463), time : time}, {
            secret : jwtConfig.refreshLogin.secret,
            expiresIn : jwtConfig.expire
        })

        return {
            accessToken,
            refreshToken,
            expire : jwtConfig.expire
        }
    }
}
